package com.xiaoyu.client.ui.activity.main.mine.my_info;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoyu.client.ClientConstants;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.FocusoncountBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.help.UserSeekInfoActivity;
import com.xiaoyu.client.ui.fragment.main.MyFragment;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2019/1/16
 * @Description 我的 -- 粉丝页面
 */
public class FansActivity extends BaseActivityWithTitle {

    @BindView(R.id.common_recycler_view_empty_page)
    ImageView mEmptyPage;//空页面
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.common_recycler_view)
    RecyclerView mRecyclerView;
    private AllPowerfulAdapter mAdapter;
    private List<FocusoncountBean.DataBean.ListBean> mFansList = new ArrayList<>();
    private boolean isAttention;//是否关注
    private String userId = "";
    private int page = 1;//当前页数
    private int totalPage;//总页数

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("我的粉丝");
        View mView = LayoutInflater.from(this).inflate(R.layout.common_recycler_view, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setPadding(0, 20, 0, 0);
        smartRefreshLayout.setOnRefreshListener(onRefreshListener);
        smartRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context).setDrawableSize(20);
            }
        });

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_SEEK_INFO) {
                userId = bundle.getString(UserSeekInfoActivity.USER_ID);
            }
            if (bundle.getInt(ClientConstants.FROM) == ClientConstants.FROM_MY) {
                userId = bundle.getString(MyFragment.USER_ID);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        mFansList.clear();
        getFocusoncount();
    }

    //获取列表数据
    private void getFocusoncount() {
        NetworkManager.attention("u", PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), "", userId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                succeedFocusonCount(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    private void succeedFocusonCount(String result) {
        FocusoncountBean bean = new Gson().fromJson(result, FocusoncountBean.class);
        mFansList.addAll(bean.getData().getList());
        totalPage = bean.getData().getCount();
        if (mFansList.size() == 0) {
            mEmptyPage.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
            mEmptyPage.setImageResource(R.mipmap.no_data);
            smartRefreshLayout.setEnableRefresh(false);
            smartRefreshLayout.setEnableLoadMore(false);
        }
        setMyAdapter(mFansList);
    }

    //设置适配器
    private void setMyAdapter(List<FocusoncountBean.DataBean.ListBean> mFocusoncountList) {
        mAdapter = new AllPowerfulAdapter<FocusoncountBean.DataBean.ListBean>(R.layout.focuson_item, mFocusoncountList) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, final FocusoncountBean.DataBean.ListBean listBean) {
                super.convert(baseViewHolder, listBean);
                //赋值显示数据
                baseViewHolder.setText(R.id.focuson_name, listBean.getNickname());
                ImageView url = baseViewHolder.getView(R.id.focuson_url);
                Glide.with(FansActivity.this).load(listBean.getUserphoto()).into(url);
                final Button mBtn = baseViewHolder.getView(R.id.focuson_btn);
                isAttention = listBean.isAttention();
                if (listBean.isAttention()) {
                    mBtn.setText("已关注");
                } else {
                    mBtn.setText("关注");
                }
                //关注按钮的点击事件
                mBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        NetworkManager.sendAttentionUser(listBean.getFocususerid(), new CommonHttpResponser() {
                            @Override
                            public void onSuccess(String result) {
                                if (isAttention) {
                                    isAttention = false;
                                    mBtn.setText("关注");
                                } else {
                                    isAttention = true;
                                    mBtn.setText("已关注");
                                }
                            }

                            @Override
                            public void onFailed(String errorCode, String errorDiscription) {
                            }
                        });
                    }
                });
            }
        };
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 数据刷新
     */
    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            page = 1;
            mFansList.clear();
            mAdapter.notifyDataSetChanged();
            getFocusoncount();
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.setNoMoreData(false);
        }
    };


    /**
     * 加载更多
     */
    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            if (page < totalPage) {
                page = page + 1;
                getFocusoncount();
                smartRefreshLayout.finishLoadMore();
                smartRefreshLayout.setNoMoreData(true);
            } else {
                ToastUtil.showToast(FansActivity.this, "已经没有更多数据！");
                smartRefreshLayout.finishLoadMore();
                smartRefreshLayout.setNoMoreData(false);
                smartRefreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    };
}
